//
//  AdGADBanner.m
//  PaintWar
//
//  Created by Tai Le on 10/13/14.
//
//

#import "AdGADBanner.h"
#import "RootViewController.h"

@implementation AdGADBanner

- (id)init
{
    self = [super init];
    if (self)
    {
        [self cycleAds];
    }
    
    return self;
}

- (void)cycleAds
{
    [super cycleAds];
    
    [self initializeAds];
    
    _gadBannerView.rootViewController = [RootViewController shared];
    
    GADRequest *request = [GADRequest request];
    
#ifndef __NDEBUG__
    request.testDevices = @[ GAD_SIMULATOR_ID, @"68a5ed350ba502938920ea2ea9b845b8", @"da26682b6f3001cee24b541b7d484d20" ];
#endif
    
    [_gadBannerView loadRequest:request];
}

- (void)initializeAds
{
    _gadBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
    _gadBannerView.adUnitID = GAD_BANNER_ID;
    _gadBannerView.delegate = self;
}

- (id)getAds
{
    return _gadBannerView;
}

- (void)releaseAds
{
    [super releaseAds];
    
    _gadBannerView.delegate = nil;
    _gadBannerView = nil;
}

#pragma mark - Delegate
- (void)adViewDidReceiveAd:(GADBannerView *)view
{
    [self raiseDidLoad:view];
}

- (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error
{
    [self raiseDidFail:view error:error];
}

- (void)adViewWillDismissScreen:(GADBannerView *)adView
{
    [self raiseDidUnload:adView];
}

@end
